<?php

namespace App\Models;

use App\Jobs\SendMessage;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class Message extends Model
{
    use  SoftDeletes;

    protected $table = 'message';

    protected $casts = [
      'publish_at'=> 'date:Y-m-d H:i',
    ];

    const TYPE =[
        1 => '普通消息',
        2 => '图文消息',
    ];

    const RECEIVER_TYPE = [
        1=> '用户',
        2=> '司机',
        3=> '公司管理员',
    ];


    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::creating(function ($model){
            $model->attributes['admin_id'] = Auth::guard('admin')->user()->id;
        });

        static::saving(function($model) {
            if(isset($model->attributes['publish_at'])) {
                $model->attributes['publish_at'] = strtotime($model->attributes['publish_at']);
            };
        });

        static::created(function ($model) {
            $message_model = new Message();
            SendMessage::dispatch($message_model->find($model->id));
        });

    }
}
